package com.qbw.app.module.vehicle.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

import com.http.okhttp.HttpTask;
import com.lib.util.ScreenUtil;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.XApplication;
import com.qbw.app.databinding.VehicleActivityNotificationSettingBinding;
import com.qbw.app.model.Vehicle;
import com.qbw.app.model.Warning;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.common.util.HttpRequestUtil;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.app.module.vehicle.adapter.VehicleAdapter;
import com.qbw.app.request.impl.VehicleRequest;
import com.qbw.core.holder.EmptyHolder;
import com.qbw.core.model.Empty;

import java.util.ArrayList;
import java.util.List;

/**
 * @author QBW
 * @date 2019/10/8
 */
public class VehicleNotificationSettingActivity extends CommonActivity<VehicleActivityNotificationSettingBinding> {

    private Vehicle mVehicle;
    private VehicleAdapter mAdapter;

    public static void start(Context context, Vehicle vehicle) {
        Intent intent = new Intent(context, VehicleNotificationSettingActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(Constant.KEY_VEHICLE, vehicle);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_activity_notification_setting;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;
        mVehicle = (Vehicle) args.getSerializable(Constant.KEY_VEHICLE);
        mBinding.title.setSubTitle(mVehicle.getVehicleNick());
        mBinding.title.setSubTitleVisible(true);
        mBinding.title.setListener(this);
        mBinding.rcv.addItemDecoration(new InnerDecoration());
        mBinding.rcv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new VehicleAdapter(this);
        mBinding.rcv.setAdapter(mAdapter);
        showLoadingDialog("");
        mGeneralManager.getRequestManager()
                       .getVehicleRequest()
                       .getWarningConfig(mVehicle.getVehicleCode(), this)
                       .execute();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(Constant.KEY_VEHICLE, mVehicle);
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_WARNING_CONFIG)) {
            mAdapter.clear();
            Warning.ResponseList response = (Warning.ResponseList) entity;
            List<Warning> safeList = new ArrayList<>();
            List<Warning> statusList = new ArrayList<>();
            if (response.size() > 0) {
                for (Warning warning : response.getData()) {
                    if (warning.getAlarmType() == Warning.NOTIFICATION_TYPE_SAFE) {
                        safeList.add(warning);
                    } else if (warning.getAlarmType() == Warning.NOTIFICATION_TYPE_VEHICLE_STATUS) {
                        statusList.add(warning);
                    }
                }
                if (safeList.size() > 0) {
                    Warning.Box box = new Warning.Box(gs(R.string.safe),
                                                      new Warning.Data(safeList));
                    mAdapter.addChild(box);
                }
                if (statusList.size() > 0) {
                    Warning.Box box = new Warning.Box(gs(R.string.vehicle_status),
                                                      new Warning.Data(statusList));
                    mAdapter.addChild(box);
                }
            } else {
                mAdapter.showEmpty(new Empty(0, EmptyHolder.ModelId.M_NO_NOTIFICATION_SET));
            }
            closeLoadingDialog();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        message = HttpRequestUtil.getMessage(errorCode, message);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_WARNING_CONFIG)) {
            UniversalToastUtil.showToast(message);
            closeLoadingDialog();
            finish();
        }
    }

    private static class InnerDecoration extends RecyclerView.ItemDecoration {

        private int mSpace1;
        private int mSpace2;

        public InnerDecoration() {
            Context context = XApplication.getInstance();
            mSpace1 = ScreenUtil.dp2px(context, 14);
            mSpace2 = ScreenUtil.dp2px(context, 10);
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect,
                                   @NonNull View view,
                                   @NonNull RecyclerView parent,
                                   @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            int ap = parent.getChildAdapterPosition(view);
            if (ap == RecyclerView.NO_POSITION) {
                return;
            }
            outRect.left = mSpace1;
            outRect.right = mSpace1;
            outRect.top = mSpace2;
        }
    }
}